package com.huluobo.report.pages

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.lib_base.BaseLoadingDialog
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.UIState
import com.huluobo.lib_http.Const
import com.huluobo.report.R
import com.huluobo.report.ReportIntent
import com.huluobo.report.ReportViewModel
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 *  Created by LC on 2025/5/29.
 */

@Composable
fun ReportCommitPage(navHostController: NavHostController, json: String, vm: ReportViewModel = hiltViewModel()) {
    val entity = Gson().fromJson(json, ReportDialogEntity::class.java)

    var title by remember {
        mutableStateOf("")
    }

    var content by remember {
        mutableStateOf("")
    }

    var isShowProgress by remember {
        mutableStateOf(false)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnError -> {
                        isShowProgress = false
                        ToastUtils.showLong("提交日志失败:${it.msg}")
                        Log.e("TagA", "提交日志失败:${it.msg}")
                    }

                    UIState.OnLoading -> {
//                        isShowProgress = true
                    }

                    is UIState.OnSuccess<*> -> {
                        isShowProgress = false
                        ToastUtils.showLong("提交成功")
                        navHostController.popBackStack()
                    }
                }
            }
        }
    })

    if (isShowProgress) {
        BaseLoadingDialog()
    }

    Column(
        Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.lightgray))
            .padding(10.dp)
    ) {
        BaseTitle(title = "日志编辑", navHostController = navHostController)
        Spacer(modifier = Modifier.height(10.dp))
        val time = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
        //通过通用工具类中的时间工具,拿到当前的周几
        val week = TimeUtils.getChineseWeek(System.currentTimeMillis())
        Column(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.White)
                .padding(start = 10.dp),
            verticalArrangement = Arrangement.Center
        ) {
            Text(text = "$time $week 今日", textAlign = TextAlign.Center)
        }

        Column(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.White)
                .padding(start = 10.dp),
            verticalArrangement = Arrangement.Center
        ) {
            Text(text = "今日内容")
        }
        Spacer(modifier = Modifier.height(10.dp))
        TextField(value = title, onValueChange = { title = it }, Modifier.fillMaxWidth(), maxLines = 1, label = {
            Text(text = "请输入${entity.name}的标题")
        }, colors = TextFieldDefaults.colors(
            //失去焦点时,容器的颜色
            unfocusedContainerColor = Color.White,
            //失去焦点时,指示器的颜色
            unfocusedIndicatorColor = Color.White,
            //获取焦点时,光标的颜色
            focusedIndicatorColor = Color.White,
            //获取焦点时,容器的颜色
            focusedContainerColor = Color.White,
            focusedLabelColor = Color.Black
        )
        )
        Spacer(modifier = Modifier.height(10.dp))
        TextField(
            value = content, onValueChange = { content = it },
            Modifier
                .fillMaxWidth()
                .height(300.dp), label = {
                Text(text = "请输入${entity.name}的内容")
            }, colors = TextFieldDefaults.colors(
                //失去焦点时,容器的颜色
                unfocusedContainerColor = Color.White,
                //失去焦点时,指示器的颜色
                unfocusedIndicatorColor = Color.White,
                //获取焦点时,光标的颜色
                focusedIndicatorColor = Color.White,
                //获取焦点时,容器的颜色
                focusedContainerColor = Color.White,
                focusedLabelColor = Color.Black
            )
        )
        Spacer(modifier = Modifier.height(10.dp))
        Button(onClick = {
            val map = mapOf(
                Const.PARAM_CONTENT to content,
                Const.PARAM_TITLE to title
            )
            //map的2种形式,等价的
//            val map2 = mutableMapOf<String, String>()
//            map2[Const.PARAM_CONTENT] = content
//            map2[Const.PARAM_TITLE] = title
            vm.sendIntent(ReportIntent.CommitReport(map))
            isShowProgress = true
        }) {
            Text(text = "提交")
        }
    }
}